<?php

namespace Home\Controller;

use Think\Controller;
use Org\Util;
class ActivityController extends Controller {
    private $WeixinLib = null;
    public function _initialize(){
        $this->WeixinLib = new Util\WeiXin();
    }
    public function actshare($bin=''){
        //获取微信签名等分享数据
        $signPackage=$this->WeixinLib ->getSignPackage();
        $this->assign('signPackage',$signPackage);
        $this->assign("WEB_SITE_TITLE","果然活动");
        //获取页面
        if($bin == '' || is_null($bin)){
            //bin必须传，否则跳转到首页
            $this->redirect("Index/index");
        }else{
            //根据bin到数据库中取值
            $Ae = new Util\AEQuery('ec','gr_activity');
            $act = $Ae->find('bin="'.$bin.'" and status=1');
            $result = Util\AETools::toArray($act);
            $content = $result[0]['content'];
            $this->assign('_content',$content); // 图文详情
            if($result[0]['share'] === 0){
                //不允许分享
                $this->display('noshare');
            }else{
                //允许分享
                $title = $result[0]['title'];
                $desc = $result[0]['desc'];
                $link = $result[0]['link'];
                $imgUrl = $result[0]['imgUrl'];
                $successUrl = $result[0]['successUrl'];
                $this->assign('_title',$title);
                $this->assign('_desc',$desc);
                $this->assign('_link',$link);
                $this->assign('_imgUrl',$imgUrl);
                $this->assign('_successUrl',$successUrl);
                $this->display();
            }

        }
    }
    public function pushpage($bin){
//        dump('first');
//        $Ae_pushed = new Util\AEQuery('shiguo','gr_pushed');
//        $where = "status=1 and bin = '".$bin."'";
//        $pushed = $Ae_pushed->find($where,1);
//        dump('second');
//        $result = Util\AETools::toArray($pushed);
        $map['status']=1;
        $map['bin']=$bin;
//        dump($map);
        $result=M('pushed')->where($map)->select();
        $content = $result[0]['content'];
        $title = $result[0]['title'];
//       dump($content);
        $this->cid=58;
        session('wxpushflag','推送');
        $this->assign("WEB_SITE_TITLE",$title);
        $this->assign("_content",$content);
        $this->display();
    }
    /**
     * 余额流水
     * @param $amount int 变动金额
     * @param $detail string 明细
     * @param $act string 操作
     * @author Scor 2015-10-10
     */
    public function amountFlow($amount,$detail,$act){
        $userid = session('userid');
        $map['id']=$userid;
        $res=M('buyers')->where($map)->find();
        $amount_arr["change_amount"]=$amount;
        $amount_arr["before_amount"]=$res["amount"];
        if($act=="incre"){
            $amount_arr["after_amount"]=$res["amount"]+$amount;
            $act="获得";
        }else{
            $act="消费";
            $amount_arr["after_amount"]=$res["amount"]-$amount;
        }
        $amount_arr["action"]=($amount_arr["after_amount"]>$amount_arr["before_amount"])?1:-1;
        $amount_arr["detail"]=$detail.$act.$amount;
        $amount_arr["uid"]=$userid;
        $amount_arr["createtime"]=time();
        if($amount>0){
            D("amount_flow")->add($amount_arr);
        }
    }
    public function isCart($pid){
        $data['pid']=$pid;
        $data['uid']=session('userid');
        $flagres=D('cart')->where($data)->select();
        if(count($flagres)==0){
            $iscartflag=false;
        }else{
            $iscartflag=true;
        }
        return $iscartflag;
    }


}
